<?php

/**
 * 首页
 */

namespace Home\Controller;

use Common\Controller\HomebaseController;

class IndexController extends HomebaseController {
    private $qi;
    private $M_award;

    //初始化
    public function _initialize() {
        parent::_initialize();
        $this->M_award = M('award_record');
    }

    //首页
    public function index() {
        $this->nowInfo();
        $this->lists(0);
        $this->display(":index");
    }

    //规则
    public function rule() {
        $this->nowInfo();
        $this->display(':rule');
    }

    //历史
    public function history() {
        $type = I('type', 0);
        $this->nowInfo();
        $this->lists($type, 1);
        $this->display(':history');
    }

    //走势
    public function trend() {
        $this->nowInfo();
        $this->lists(0);
        $this->display(":trend");
    }

    //现在信息
    private function nowInfo() {
        $start_date = date('Ymd');

        $start_time = strtotime(date('Y-m-d 10:00:00'));

        $now_time = time();

        //间隔秒
        $jian_ge_second = (get_jian_ge_minute() * 60);

        //现在期数
        // $qi = intval(($now_time - $start_time) / $jian_ge_second + 1);
        //当前期数
        $row = floor(($now_time - $start_time) % 86400 / 60 / $jian_ge_second) + 1;
        if($row == 0){
            $qi =floor(($now_time - $start_time) % 86400 / 60 / $jian_ge_second) + 1;
        }else{
            $qi =floor(($now_time - $start_time) % 86400 / $jian_ge_second) + 1;
        }

        $this->qi = $qi;
        $now_qi_shu = $start_date . sprintf('%03d', $qi);
        $this->assign('now_qi_shu', $now_qi_shu);

        //现在开奖号
        $now_kai_jiang_hao = $this->M_award->where("qishu='{$now_qi_shu}'")->find();
        $this->assign('now_kai_jiang_hao', $now_kai_jiang_hao);


        //倒计时
        $dao_ji_shi = $jian_ge_second - ($now_time - ($qi - 1) * $jian_ge_second) + $start_time;
        $this->assign('dao_ji_shi', $dao_ji_shi);

        //倒计时展示
        $dao_ji_shi_show = sprintf('%02d', $dao_ji_shi / 60) . ":" . sprintf('%02d', $dao_ji_shi % 60);
        $this->assign('dao_ji_shi_show', $dao_ji_shi_show);

        //下一期数
        if ($qi == intval(1440 / (get_jian_ge_minute()))) {
            $qi = 0;
            $start_date = date('Ymd', strtotime('+1 day'));
        }
        $next_qi_shu = $start_date . sprintf('%03d', $qi + 1);
        $this->assign('next_qi_shu', $next_qi_shu);
    }

    //开奖列表
    private function lists($type, $history = 0) {
        $all_total = intval(1440 / (get_jian_ge_minute()));

        if ($type == 0) {
            $where['create_time'] = array('lt', date('Y-m-d H:i:s'));
            $show_row = 50;
            $limit = $show_row;
            
            // if ($history) {
            //     $limit = ($show_row) . ',' . ($this->qi - $show_row);
            // } else {
            //     $limit = $show_row;
            // }
        } else {

            $time = strtotime('-' . $type . ' day');
            $where['create_time'] = array('between', array(date('Y-m-d 00:00:00', $time), date('Y-m-d 23:59:59', $time)));
            $limit = $all_total;
        }

        $lists = $this->M_award
            ->where($where)
            ->limit($limit)
            ->order('create_time DESC')
            ->select();

        $this->assign('lists', $lists);
        $this->assign('type', $type);
    }
}
